#include <AknIconUtils.h>
#include <AknIconArray.h>
#include <AknLists.h>
#include <aknnavi.h>

#include <BADESCA.H>
#include <GULICON.H>
#include <EIKCLBD.H>
#include <aknnavide.h>
#include <aknsfld.h>

#include <avkon.mbg>
#include "Utils.h"

#include "MySelectionDialog.h"
#include "Logger.h"

_LIT(KAvkonIconsFileName, "z:\\resource\\apps\\avkon2.mif");

CMySelectionListDialog::~CMySelectionListDialog()
{
	if( iNaviTabs )
	{
		CAknNavigationControlContainer* np = ( CAknNavigationControlContainer* ) CEikonEnv::Static()->AppUiFactory()->StatusPane()->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) );
		np->Pop();
		delete iNaviTabs;
		iNaviTabs = NULL;
	}

	iSelectedItems.Close();
	iItemsUids.Close();
}

CMySelectionListDialog::CMySelectionListDialog( TInt& aIndex, RArray< TUid >& aMenuItems, CDesCArray* aArray, MEikCommandObserver* aCommand )
: CAknSelectionListDialog( aIndex, aArray, NULL ), iListBoxItems( aArray ), iMenuItems( aMenuItems )
{
}

void CMySelectionListDialog::LoadIconsL()
{
	CArrayPtr<CGulIcon>* icons = new( ELeave ) CAknIconArray( 1 );

	CFbsBitmap*	icon( NULL );
	CFbsBitmap*	iconMask( NULL );

	AknIconUtils::CreateIconL( icon, iconMask, KAvkonIconsFileName, EMbmAvkonQgn_prop_checkbox_off, EMbmAvkonQgn_prop_checkbox_off+1);
	CGulIcon* gulIcon = CGulIcon::NewL(icon, iconMask);
	icons->AppendL( gulIcon );

	AknIconUtils::CreateIconL( icon, iconMask, KAvkonIconsFileName, EMbmAvkonQgn_prop_checkbox_on, EMbmAvkonQgn_prop_checkbox_on+1);
	gulIcon = CGulIcon::NewL(icon, iconMask);
	icons->AppendL( gulIcon );
	
	SetIconArrayL( icons );
}

void CMySelectionListDialog::MarkCurrentItem()
{
	TInt ind = ListBox()->CurrentItemIndex();
	if( ind == KErrNotFound )
		return;

	CAknFilteredTextListBoxModel* listboxModel = static_cast< CAknFilteredTextListBoxModel* >( ListBox()->Model() );
	TInt realInd = listboxModel->Filter()->FilteredItemIndex( ind );
 
	TBuf< 100 > newItem;
	newItem.Copy( (*iListBoxItems)[ realInd ]);

	if( newItem[ 0 ] == '0' )
	{
		iSelectedItems.Append( iItemsUids[ realInd ] );
		newItem[ 0 ] = '1';
	}
	else
	{
		TInt ind = iSelectedItems.Find( iItemsUids[ realInd ] );
		if( ind != KErrNotFound )
			iSelectedItems.Remove( ind );
		newItem[ 0 ] = '0';
	}

	iListBoxItems->Delete( realInd );
	iListBoxItems->InsertL( realInd, newItem );
	ListBox()->DrawNow();
}

void CMySelectionListDialog::NextPageL(TInt aScroll)
{
	if( iListBox->CurrentItemIndex() == KErrNotFound || iListBoxItems->Count() == 0 )
		return;

	TInt itemIndex = Min(iListBox->Model()->NumberOfItems()-1, Max(0, iListBox->CurrentItemIndex()+aScroll));
	TInt topIndex = Min(iListBox->Model()->NumberOfItems()-1, Max(0, iListBox->TopItemIndex()+aScroll));

	iListBox->SetTopItemIndex(topIndex );
	iListBox->SetCurrentItemIndex(itemIndex);
	iListBox->DrawNow();
}

void CMySelectionListDialog::AddTitleL( const TDesC& aTitle )
{
	CAknNavigationControlContainer* np = ( CAknNavigationControlContainer* ) CEikonEnv::Static()->AppUiFactory()->StatusPane()->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) );
	iNaviTabs = np->CreateNavigationLabelL( aTitle );
	np->PushL( *iNaviTabs );
}

TBool CMySelectionListDialog::OkToExitL( TInt aButtonId )
{
	LOG_FORMAT( _L("CMySelectionListDialog::OkToExitL(%d)"), aButtonId );

	TBool res = CAknSelectionListDialog::OkToExitL( aButtonId );

	if( aButtonId == EAknSoftkeySelect )
		MarkCurrentItem();
	if( aButtonId == EAknSoftkeyOk )
	{
		//delete the unmarked items
		for( TInt i = 0; i < iMenuItems.Count(); i++ )
		{
			TBool found = KErrNotFound != iSelectedItems.Find( iMenuItems[ i ] );
			if( !found )
			{
				iMenuItems.Remove( i );
				i--;
			}
		}

		//add new items
		for( TInt i = 0; i < iSelectedItems.Count(); i++ )
		{
			TBool found = EFalse;
			for( TInt j = 0; j < iMenuItems.Count(); j++ )
			{
				if( iMenuItems[ j ] == iSelectedItems[ i ] )
				{
					found = ETrue;
					break;
				}
			}
			if( !found )
			{
				iMenuItems.Append( iSelectedItems[ i ] );
			}
		}

		return ETrue;
	}
	return EFalse;
}

void CMySelectionListDialog::PreLayoutDynInitL()
{
	CAknSelectionListDialog::PreLayoutDynInitL();

	//change the id of the button, so we can use it easy in OkToExitL
/*	CEikButtonGroupContainer* cba = CEikButtonGroupContainer::Current();
	if( cba )
	{
		MEikButtonGroup* buttonGroup = cba->ButtonGroup();
		for( TInt pos = 0; pos < 3; pos++ )
		{
			TInt cmdId = buttonGroup->CommandId( pos );
			if( cmdId == EAknSoftkeyOk )
			{
				CCoeControl* button = buttonGroup->GroupControlById( cmdId );
				if( button && buttonGroup->IsCommandVisible( cmdId ))
				{
					CEikLabel* label = static_cast<CEikLabel*>( button->ComponentControl(0) );
					TBuf< 100 > text = *label->Text();
					cba->RemoveCommandFromStack( pos, cmdId );
					cba->SetCommandL( pos, EAknSoftkeySave, text); //why!
				}
				break;
			}
		}
	}*/
}
